home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / GlyphaIV / G4Main.c < prev    next >
Encoding:
Text File  |  2000-09-28  |  4.2 KB  |  156 lines  |  [TEXT/MPS ]

  1.  
  2. //============================================================================
  3. //----------------------------------------------------------------------------
  4. //                                Glypha IV 1.0
  5. //                                by  Scheherazade
  6. //----------------------------------------------------------------------------
  7. //============================================================================
  8.  
  9. // Here is the "main" file for Glypha.  Here is where the game begins and ends.
  10. // Also included are the preference calls.
  11.  
  12. #include <Sound.h>
  13.  
  14. #include "G4Externs.h"
  15.  
  16.  
  17. #define kPrefsVersion            0x0002
  18.  
  19.  
  20. void ReadInPrefs (void);
  21. void WriteOutPrefs (void);
  22. void main (void);
  23.  
  24.  
  25. prefsInfo    thePrefs;
  26. long        wasVolume;
  27.  
  28. extern    Boolean        quitting, playing, pausing, evenFrame;
  29. void UpdateMainWindow (void);
  30.  
  31. //==============================================================  Functions
  32. //--------------------------------------------------------------  ReadInPrefs
  33.  
  34. //    This function loads up the preferences.  If the preferences 
  35. //    aren't found, all settings are set to their defaults.
  36.  
  37. void ReadInPrefs (void)
  38. {
  39.     short        i;
  40.                             // Call LoadPrefs() function - returns TRUE if it worked.
  41.     if (LoadPrefs(&thePrefs, kPrefsVersion))
  42.         SetDefaultOutputVolume(thePrefs.wasVolume);
  43.     else                    // If LoadPrefs() failed, set defaults.
  44.     {
  45.         thePrefs.prefVersion = kPrefsVersion;                // version of prefs
  46.         thePrefs.filler = 0;                                // just padding
  47.         PasStringCopy("\pYour Name", thePrefs.highName);    // last highscores name
  48.         for (i = 0; i < 10; i++)                            // loop through scores
  49.         {
  50.             PasStringCopy("\pNemo", thePrefs.highNames[i]);    // put "Nemo" in name
  51.             thePrefs.highScores[i] = 0L;                    // set highscore to 0
  52.             thePrefs.highLevel[i] = 0;                        // level attained = 0
  53.         }
  54.         GetDefaultOutputVolume (&thePrefs.wasVolume);            // get new volume
  55. //        GetSoundVol(&thePrefs.wasVolume);
  56.     }
  57.                             // Get sound volume so we can restore it.
  58.     GetDefaultOutputVolume (&wasVolume);            // get new volume
  59. //    GetSoundVol(&wasVolume);
  60. }
  61.  
  62. //--------------------------------------------------------------  WriteOutPrefs
  63.  
  64. //    This function writes out the preferences to disk and restores 
  65. //    the sound volume to its setting before Glypha was launched.
  66.  
  67. void WriteOutPrefs (void)
  68. {
  69.     if (!SavePrefs(&thePrefs, kPrefsVersion))
  70.         SysBeep(1);
  71.     SetDefaultOutputVolume(wasVolume);
  72. }
  73.  
  74. //--------------------------------------------------------------  main
  75.  
  76. //    This is the main function.  Every C program has one of these.
  77. //    First it initializes our program and then falls into a loop
  78. //    until the user chooses to quit.  At that point, it cleans up
  79. //    and exits.
  80.  
  81. void main (void)
  82. {
  83.     long        tickWait;
  84.     OSStatus    theError;
  85.     Boolean        wasPlaying = false;
  86.     
  87.     // startup DrawSprocket
  88.     theError = DSpStartup();
  89.     if( theError )
  90.         RedAlert("\punable to init DrawSprocket!");
  91.     // DSpSetDebugMode( true );
  92.     
  93.     ToolBoxInit();            // Call function that initializes the ToolBox managers.
  94.     CheckEnvirons();        // Check the Mac we're on to see if we can run.
  95.     OpenMainWindow();        // Open up the main window - it will fill the monitor.
  96.     InitVariables();        // Initialize Glypha's variables.
  97.     InitSound();            // Create sound channels and load up sounds.
  98.     InitMenubar();            // Set up the game's menubar.
  99.     ReadInPrefs();            // Load up the preferences.
  100.  
  101. #if GENERATINGPOWERPC
  102.     UpdateMainWindow();
  103.  
  104.     theError = DSpContext_FadeGammaIn( NULL, NULL );
  105.     if( theError )
  106.         RedAlert("\pUnable to unfade the display!");
  107.     
  108. #endif
  109.  
  110.     do                        // Here begins the main loop.
  111.     {
  112.     
  113.         HandleEvent();        // Check for events.
  114.         
  115.         if ((playing) && (!pausing))
  116.         {
  117.             wasPlaying = true;
  118.             HideCursor();
  119.             PlayGame();        // If user began game, drop in game loop. (play mode)
  120.             ShowCursor();
  121.         }
  122.         else if (pausing && playing)
  123.         {
  124.         }
  125.         else                // If no game, animate the screen. (idle mode)
  126.         {
  127.             if (Button())
  128.             {
  129.                 Point pt;
  130.                 
  131.                 GetMouse(&pt);
  132.                 pt.h += (Random() % 21) - 10;
  133.                 pt.v += (Random() % 21) - 10;
  134.                 StartPixelShatter(pt.h, pt.v, 0, 0, kShatterLightningDust);
  135.             }
  136.             
  137.             if (wasPlaying)
  138.             {
  139.                 GameQuitGraphics();
  140.                 wasPlaying = false;
  141.             }
  142.             tickWait = TickCount() + 2L;
  143.             GameIdleAnimation();
  144.  
  145.             evenFrame = !evenFrame;
  146.         }
  147.     }
  148.     while (!quitting);
  149.     
  150.     KillSound();            // Dispose of sound channels.
  151.     ShutItDown();            // Dispose of other structures.
  152.     WriteOutPrefs();        // Save preferences to disk.
  153.  
  154. }
  155.  
  156.